<template>
  <form class="search-bar" @submit.prevent="change">
    <input
      type="text"
      placeholder="搜索超市商品"
      v-model="val"
    />
    <a
      v-show="val"
      href="javascript:;"
      class="close"
      @click="clear"
    >
      <icon name="guanbi" :size="40" />
    </a>
  </form>
</template>

<script>
export default {
  name: 'SearchBar',
  props: {
    value: String,
  },
  data () {
    return {
      val: this.value || ''
    }
  },
  watch: {
    value (newValue) {
      this.val = newValue
    },
    val (newValue) {
      this.$emit('input', newValue)
    },
  },
  methods: {
    gotosearch () {
      this.$router.push('/search')
    },
    clear () {
      this.val = ''
      this.$emit('onClear')
    },
    change (e) {
      this.$emit('change', e)
    }
  }
}
</script>

<style lang="scss" scoped>
.search-bar {
  @include wh(100%, 98px);
  @include flex(row, center, center);
  @include border1px(bottom, #E2E2E2);
  background: #fff;
  flex-shrink: 0;
  position: relative;
  input {
    @include wh(auto, 64px);
    flex: 1;
    margin: 0 30px;
    padding: 0 40px;
    border-radius: 64px;
    background: #F6F6F6;
    border: 0;
    outline: none;
  }
  .close {
    position: absolute;
    right: 40px;
  }
}
</style>
